<template>
    <view v-if="isPopupVisible" 
          class="down-popup-mask" 
          :style="maskStyle"
          @click="startHideAnimation"
    >
        <view 
            class="down-popup-content" 
            :class="{ 'slide-down': value && !isClosing, 'slide-up': isClosing }" 
            @click.stop
        >
            <view class="history-keyword">
                <view class="history-title space-between">
                    <text>历史搜索</text>
                    <text @tap="clearHistory">清空</text>
                </view>
                <view class="history-tag-list">
                    <view v-for="(item, index) in historyList" 
                          :key="index" 
                          @tap="clickTaghandler(item)"
                    >{{ item }}</view>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
const HISTORY_KEY = 'history_list'

export default {
    name: 'HistoryPopup',
    props: {
        value: {
            type: Boolean,
            default: false
        },
        topDistance: {
            type: [Number, String],
            default: 0
        }
    },
    computed: {
        maskStyle() {
            return {
                top: `${this.topDistance}rpx`,
                height: `calc(100vh - ${this.topDistance}rpx)`
            }
        }
    },
    data() {
        return {
            historyList: [],
            isPopupVisible: false,
            isClosing: false
        }
    },
    watch: {
        value: {
            immediate: true,
            handler(newVal) {
                if (newVal) {
                    this.isPopupVisible = true
                    this.isClosing = false
                } else {
                    this.startHideAnimation()
                }
            }
        }
    },
    created() {
        this.loadHistory()
    },
    methods: {
        startHideAnimation() {
            if (!this.isPopupVisible) return
            this.isClosing = true
            setTimeout(() => {
                this.isPopupVisible = false
                this.isClosing = false
                this.$emit('input', false)
            }, 300)
        },
        loadHistory() {
            try {
                const history = uni.getStorageSync(HISTORY_KEY)
                if (history) {
                    this.historyList = history
                }
            } catch (e) {
                console.error('获取搜索历史失败:', e)
            }
        },

        saveSearchHistory(keyword) {
            try {
                let history = this.historyList.filter(item => item !== keyword)
                history.unshift(keyword)
                history = history.slice(0, 10)

                this.historyList = history
                uni.setStorageSync(HISTORY_KEY, history)
            } catch (e) {
                console.error('保存搜索历史失败:', e)
            }
        },

        clearHistory() {
            try {
                this.historyList = []
                uni.removeStorageSync(HISTORY_KEY)
                uni.showToast({
                    title: '已清空历史记录',
                    icon: 'none'
                })
                this.$parent.checkHistory()
            } catch (e) {
                console.error('清除搜索历史失败:', e)
                uni.showToast({
                    title: '清除历史失败',
                    icon: 'none'
                })
            }
        },

        clickTaghandler(content) {
            this.$emit('doSearch', {
                value: content
            })
        }
    }
}
</script>

<style lang="scss">
.down-popup-mask {
    position: fixed;
    left: 0;
    width: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
}

.down-popup-content {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    background-color: white;
    z-index: 1000;
    transform: translateY(-100%);
}

.slide-down {
    animation: slide-down 0.3s ease-out forwards;
}

.slide-up {
    animation: slide-up 0.3s ease-in forwards;
}

@keyframes slide-down {
    0% {
        transform: translateY(-100%);
    }
    100% {
        transform: translateY(0);
    }
}

@keyframes slide-up {
    0% {
        transform: translateY(0);
    }
    100% {
        transform: translateY(-100%);
    }
}

.history-keyword {
    padding: 30rpx;

    .history-title {
        font-size: 30rpx;
        color: #222222;
        display: flex;
        justify-content: space-between;
        align-items: center;

        text:last-child {
            color: #999;
        }
    }

    .history-tag-list {
        display: flex;
        flex-wrap: wrap;
        margin-top: 30rpx;

        view {
            font-size: 25rpx;
            color: #999;
            border: 1rpx solid #999;
            padding: 6rpx 15rpx;
            margin: 15rpx;
            overflow: hidden;
            white-space: nowrap;
            text-overflow: ellipsis;
        }
    }
}
</style>